package cn.topevery.garden.wapi.controller;

import cn.topevery.garden.dal.dao.impl.GreTreeFamousDaoImpl;
import cn.topevery.garden.dal.logic.GreFileLogic;
import cn.topevery.garden.dal.logic.GreTreeFamousLogic;
import cn.topevery.garden.dal.logic.InteractPlatformFileLogic;
import cn.topevery.garden.dal.logic.InteractPlatformLogic;
import cn.topevery.garden.entity.dto.InteractPlatformDTO;
import cn.topevery.garden.entity.po.GreTreeFamous;
import cn.topevery.garden.entity.po.InteractPlatform;
import cn.topevery.garden.entity.query.GreTreeFamousQuery;
import cn.topevery.garden.entity.vo.GreTreeFamousVO;
import cn.topevery.garden.wapi.utils.CoordinateTransHelper;
import com.github.dozermapper.core.Mapper;
import com.github.dozermapper.core.MappingException;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.dto.PointDto;
import hw.topevery.framework.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 古树名木表
 *
 * @author code-generate
 * @date 2022-01-05
 */
@Api(tags = "免验证")
@RestController
@RequestMapping("/demo/greTreeFamous")
public class HfGreTreeFamousController extends BaseController {

    @Autowired
    private Mapper mapper;

    @Autowired
    GreTreeFamousLogic greTreeFamousLogic;

    @Autowired
    GreFileLogic greFileLogic;


    @Autowired
    GreTreeFamousDaoImpl greTreeFamousDao;

    @Autowired
    InteractPlatformLogic interactPlatformLogic;

    @Autowired
    InteractPlatformFileLogic interactPlatformFileLogic;


    /**
     * 获取古树名木表分页列表
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "获取古树名木记录表页列表")
    @PostMapping("/getList")
    public JsonResult<DataPager<GreTreeFamousVO>> getList(@RequestBody GreTreeFamousQuery query) {
        DataPager<GreTreeFamousVO> res = greTreeFamousLogic.listPage(query);
        List<GreTreeFamousVO> records = res.records;
        if (records != null && !records.isEmpty()) {
            records.stream().peek(temp -> {
                temp.setFileList(greFileLogic.getListByMainId(temp.getId(),3));
            }).collect(Collectors.toList());
        }
        return JsonResult.ok(res);
    }


    @GetMapping(value = "/getById")
    @ApiOperation(value = "通过id获取树木信息")
    public JsonResult<GreTreeFamous> getById(@RequestParam  Integer id) {
        GreTreeFamous res = greTreeFamousLogic.getEntity(id);
        res.setFileList(greFileLogic.getListByMainId(res.getId(), 3));
        return JsonResult.ok(res);
    }

    @ApiOperation("通过Id获取详情")
    @GetMapping("/getInfoById")
    public JsonResult<GreTreeFamousVO> getInfoById(@RequestParam Integer id) {
        GreTreeFamous entity = greTreeFamousLogic.getEntity(id);
        GreTreeFamousVO vo= mapper.map(entity, GreTreeFamousVO.class);
        return JsonResult.ok(vo);
    }



    @GetMapping(value = "/changeAbs")
    @ApiOperation(value = "转坐标")
    public JsonResult<?> changeAbs() {
        Map<String, Object> map = greTreeFamousDao.getOneMap("select geom from t_gis_map_mask where code = 'baidu'");
        Object object = map.get("geom");
        List<List<PointDto>> data = (List<List<PointDto>>) object;
        List<PointDto> pointDtos = data.get(0);
        StringBuilder geom = new StringBuilder();
        geom.append("ST_GeomFromText('POLYGON((");
        for (int i=0;i< pointDtos.size();i++){
            PointDto pointDto =  CoordinateTransHelper.bd09WGS84(pointDtos.get(i).getLng(),pointDtos.get(i).getLat());
            if (i==pointDtos.size()-1){
                geom.append(pointDto.getLng()+" "+ pointDto.getLat());
            }else {
                geom.append(pointDto.getLng()+" "+ pointDto.getLat()+", ");
            }
        }
        geom.append("))')");
        greTreeFamousDao.updateToWgs(geom);
        return JsonResult.ok("操作成功");

    }

    @ApiOperation("新增/编辑 互动平台信息")
    @PostMapping("/saveOrUpdate")
    public JsonResult<Boolean> saveOrUpdate(@Valid @RequestBody InteractPlatformDTO dto) {
        InteractPlatform interactPlatform = mapper.map(dto, InteractPlatform.class);
        GreTreeFamous greTreeFamous = greTreeFamousLogic.getById(dto.getTreeId());
        interactPlatform.setStatus(0).setTreeName(greTreeFamous.getName()).setTreeCode(greTreeFamous.getCode()).setLat(greTreeFamous.lat).setLng(greTreeFamous.lng).setAddress(greTreeFamous.address);
        if (interactPlatform.getId() == null) {
            interactPlatform.setDbStatus(false);
        }
        try {
            interactPlatformLogic.saveOrUpdate("66", interactPlatform);
            interactPlatformFileLogic.saveOrUpdate("66", interactPlatform.getId(), interactPlatform.getFileList());
            return JsonResult.ok(true);
        } catch (MappingException e) {
            e.printStackTrace();
            return JsonResult.ok(false);
        }
    }

}
